﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BugTracker.Models;
using BugTracker.Helper;
using System.Data;
using System.Data.Entity;
using BugTracker.Controllers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
//using System.Data.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Text;
using System.IO;
using System.Data.Objects;
using System.Data.Entity.Infrastructure;

namespace BugTracker.Controllers
{
    public class ReportController : Controller
    {
        //
        // GET: /Report/
        Bug_db toget = new Bug_db();
        SessionHelper sessionhelper = new SessionHelper();

        public ActionResult Issue()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View(toget.Issues.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        public ActionResult ExportToExcelIssue()
        {
            {
                var documents = toget.Issues as IEnumerable<Issue>;

                var grid = new GridView
                {
                    DataSource = from document in documents
                                 select new
                                 {
                                     IssueID = document.issue_idPK,
                                     IssueName = document.issue_name,
                                     UserID = document.user_idFK,
                                     IssueSummary = document.issue_summary,
                                     ActualDateResolution = document.actual_resolutiondate.ToString("MM/dd/yyyy"),
                                     Priority = document.priority,
                                     Status = document.status,
                                     ProjectID = document.project_idFK,
                                     DateIdentified = document.date_identified
    
                                 }
                };

                grid.DataBind();

                string attachment = "attachment; filename=Issue.xls";

                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-Disposition", attachment);
                Response.AppendHeader("content-length", attachment.Length.ToString());
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                Response.Charset = "";

                StringWriter sw = new StringWriter();

                HtmlTextWriter htw = new HtmlTextWriter(sw);

                grid.RenderControl(htw);

                Response.Write(sw.ToString());

                Response.End();
                return new EmptyResult();
            }
        }


        public ActionResult Project()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View(toget.Projects.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }


        public ActionResult ExportToExcelProject()
        {
            {
                var documents = toget.Projects as IEnumerable<Project>;

                var grid = new GridView
                {
                    DataSource = from document in documents
                                 select new
                                 {
                                     ProjectID = document.project_idPK,
                                     AssignID = document.assign_idFK,
                                     UserID = document.user_idFK,
                                     ProjectName = document.project_name,
                                     ProjectDateCreated = document.project_datecreated,
                                     ProjectDeadline = document.project_deadline.ToString("MM/dd/yyyy"),
                                     ProjectDescription = document.project_description,
                                     TaskAssign = document.task_assign,
                                     IssueID = document.issue_idFK,
                                     StatusID = document.status_id
                                 }
                };

                grid.DataBind();

                string attachment = "attachment; filename=Project.xls";

                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-Disposition", attachment);
                Response.AppendHeader("content-length", attachment.Length.ToString());
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                Response.Charset = "";

                StringWriter sw = new StringWriter();

                HtmlTextWriter htw = new HtmlTextWriter(sw);

                grid.RenderControl(htw);

                Response.Write(sw.ToString());

                Response.End();
                return new EmptyResult();
            }
        }

        public ActionResult User()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View(toget.Users.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        public ActionResult ExportToExcelUser()
        {
            {
                var documents = toget.Users as IEnumerable<User>;

                var grid = new GridView
                {
                    DataSource = from document in documents
                                 select new
                                 {
                                     UserID = document.user_idPK,
                                     RoleID = document.role_idFK,
                                     UserName = document.user_name,
                                     Password = document.password,
                                     FirstName = document.first_name,
                                     LastName = document.last_name,
                                     DateCreated = document.datecreated

                                 }
                };

                grid.DataBind();

                string attachment = "attachment; filename=User.xls";

                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-Disposition", attachment);
                Response.AppendHeader("content-length", attachment.Length.ToString());
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                Response.Charset = "";

                StringWriter sw = new StringWriter();

                HtmlTextWriter htw = new HtmlTextWriter(sw);

                grid.RenderControl(htw);

                Response.Write(sw.ToString());

                Response.End();
                return new EmptyResult();
            }
        }
    }
}
